<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9 0009
 * Time: 下午 2:50
 */
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Module;

class Base extends Controller{
	public function _initialize()
    {
    	parent::_initialize();
        $session_admin_id = session('TENANT_ID');
        if (!empty($session_admin_id)) {
            $user = model('operator')->where(['id' => session('member')['id']])->find()->toArray();

            if (!$this->checkAccess()) {
                $this->error("您没有访问权限！");
            }
            $this->assign("admin", $user);
            $member = session('member');
            $moduleMdl = new Module();
            
            if($member['roleType'] === 0 ) {  //超级管理员登录，有全部权限
                $modules = $moduleMdl->toTree('',true);
            } else {  //根据权限取对应菜单
                $modules = $moduleMdl->toTree($member['moduleIds']);
            }
            
            $this->assign('modules',$modules);
        } else {
            if ($this->request->isPost()) {
                $this->error("您还没有登录！", url("admin/login/index"));
            } else {
                header("Location:" . url("admin/login/index"));
                exit();
            }
        }
    }

    private function checkAccess() {
        // 若登录账号为超级管理员，则不需要验证
        $user = session('member');
        if ($user['roleType'] === 0) {
            return true;
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        // $action     = $this->request->action();  
        //不验证操作方法，默认能访问本控制器内所有方法，实际功能实现也请以一个权限对应一个控制器

        $path = strtolower($module.'/'.$controller.'/'.'index');
        if($path == 'admin/index/index')
        	return true;
        $module_id = model('module')->where(['path'=>$path])->find();   //欲访问模块的ID

        if($module_id) {
        	if(!in_array($module_id['id'],explode(',',$user['moduleIds'])) || !stripos($module_id['path'],$controller)) {
        		return false;
        	} else {
        		return true;
        	}
        }

        return false;



    }

    public function uploadfile(){
	    return \app\lib\upload\UploadFile::UploadImg();
    }


}